Skip to main content

Keyboard Row

LeetCode 500 | Difficulty: Easy​

Easy

Problem Description​

Given an array of strings words, return the words that can be typed using letters of the alphabet on only one row of American keyboard like the image below.

Note that the strings are case-insensitive, both lowercased and uppercased of the same letter are treated as if they are at the same row.

In the American keyboard:

- the first row consists of the characters `"qwertyuiop"`,

- the second row consists of the characters `"asdfghjkl"`, and

- the third row consists of the characters `"zxcvbnm"`.

Example 1:

Input: words = ["Hello","Alaska","Dad","Peace"]

Output: ["Alaska","Dad"]

Explanation:

Both "a" and "A" are in the 2nd row of the American keyboard due to case insensitivity.

Example 2:

Input: words = ["omk"]

Output: []

Example 3:

Input: words = ["adsdf","sfd"]

Output: ["adsdf","sfd"]

Constraints:

- `1 <= words.length <= 20`

- `1 <= words[i].length <= 100`

- `words[i]` consists of English letters (both lowercase and uppercase).

Topics: Array, Hash Table, String


Approach​

Hash Map​

Use a hash map for O(1) average lookups. Store seen values, frequencies, or indices. The key question: what should I store as key, and what as value?

When to use

Need fast lookups, counting frequencies, finding complements/pairs.

String Processing​

Consider character frequency counts, two-pointer approaches, or building strings efficiently. For pattern matching, think about KMP or rolling hash. For palindromes, expand from center or use DP.

When to use

Anagram detection, palindrome checking, string transformation, pattern matching.


Solutions​

Solution 1: C# (Best: 228 ms)​

MetricValue
Runtime228 ms
Memory42.9 MB
Date2022-02-02
Solution
public class Solution {
public string[] FindWords(string[] words) {
string option1 = "qwertyuiop";
string option2 = "asdfghjkl";
string option3 = "zxcvbnm";
List<string> result = new List<string>();
foreach (var word in words)
{
var word1 = word.ToLower();
if (!word1.Where(x => !option1.Contains(x)).Any() ||
!word1.Where(x => !option2.Contains(x)).Any() ||
!word1.Where(x => !option3.Contains(x)).Any())
result.Add(word);
}
return result.ToArray();
}
}
πŸ“œ 1 more C# submission(s)

Submission (2022-02-02) β€” 315 ms, 42.9 MB​

public class Solution {
public string[] FindWords(string[] words) {
string option1 = "qwertyuiop";
string option2 = "asdfghjkl";
string option3 = "zxcvbnm";
List<string> result = new List<string>();
foreach (var word in words)
{
var word1 = word.ToLower();
if (word1.Where(x => !option1.Contains(x)).ToList().Count == 0) result.Add(word);
if (word1.Where(x => !option2.Contains(x)).ToList().Count == 0) result.Add(word);
if (word1.Where(x => !option3.Contains(x)).ToList().Count == 0) result.Add(word);
}
return result.ToArray();
}
}

Complexity Analysis​

ApproachTimeSpace
Hash Map$O(n)$$O(n)$

Interview Tips​

Key Points
  • Start by clarifying edge cases: empty input, single element, all duplicates.
  • Hash map gives O(1) lookup β€” think about what to use as key vs value.